jQuery(document).ready(function() {

    /*** NyroMoadal ajax call   **/
    jQuery('.nyromodal').nyroModal();

    /*** FORMULAIRE RECHERCHE ***/
    // Autocomplete
    if(autoComplete != null){
       jQuery(autoComplete.element).autocomplete(autoComplete.data);
       jQuery(autoComplete.element).keyup(function() {
            var value = jQuery(this).val().length;
           if(value >= 2)
           jQuery(this).addClass('loading');
        });
    }
    /*** / FORMULAIRE RECHERCHE ***/

    /*** Google MAP
     * http://gmap.nurtext.de/examples.html
     */
    if(GoogleMap != null){
            jQuery("#"+GoogleMap.id).gMap({
                markers: GoogleMap.markers,
                zoom: GoogleMap.zoom }
            );
    }

    /***
     * Google MAP Poly
     * http://www.devweb-decouverte.fr/utiliser-canvas-dans-google-maps-pour-afficher-des-departements/
     */
    if(GooglePoly != null){
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("amap"));
            map.setUIToDefault();
            jQuery.each( GooglePoly, function(i, l){
                GDownloadUrl(l.file,
                             function(data, responseCode) {
                                 // xmlData = google.maps.Xml.parse(data);
                                 var xmlData = GXml.parse(data);
                                 var coords = xmlData.documentElement.getElementsByTagName("coordinate");
                                 var coordinates = new Array();
                                 for (var i = 0; i < coords.length; i++) {
                                     coordinates[i] = new GLatLng(parseFloat(coords[i].getAttribute("y")),parseFloat(coords[i].getAttribute("x")));
                                 }
                                 coordinates[i+1] = coordinates[0];

                                 map.setCenter(new GLatLng(coordinates[0]['Kd'],coordinates[0]['Ga']),l.zoom);

                                 polygons = new google.maps.Polygon(coordinates,"#000000", 0.5, 1, l.color, 0.2);
                                 map.addOverlay(polygons);
                             });
            });
        }
    }
    /*** / Google MAP **/
    
    /*** Listing Ajax ***/
    listingPagination();
    
    
    
    jQuery('#errorAction').click();

    if(ChartData != null)
        new Highcharts.Chart(ChartData);


    jQuery(".jRating").jRating({
        bigStarsPath:BASE_URL+'themes/notation/icons/stars.png',
        phpPath:BASE_URL+'services/notation/'
    });

    jQuery("#magNote .jRating").jRating({
        bigStarsPath:BASE_URL+'themes/notation/icons/stars.png',
        phpPath:BASE_URL+'services/notation/magasin/'
    });

    jQuery("#magNote .jRating1").jRating({
        bigStarsPath:BASE_URL+'themes/notation/icons/stars.png',
        phpPath:BASE_URL+'services/notation/magasin/1'
    });

    jQuery("#magNote .jRating2").jRating({
        bigStarsPath:BASE_URL+'themes/notation/icons/stars.png',
        phpPath:BASE_URL+'services/notation/magasin/2'
    });

    jQuery("#magNote .jRating3").jRating({
        bigStarsPath:BASE_URL+'themes/notation/icons/stars.png',
        phpPath:BASE_URL+'services/notation/magasin/3'
    });


    jQuery('a').click(function (){_gaq.push(['_trackEvent', 'Clic', 'action']);})
});



// Select one entry
function submitQuery(element, value,data){
    if(element == undefined)
        element = '#query.search_field';
    jQuery(element).parent().find('.return').val(data);
    jQuery(element).parent().submit();
}


/*** Listing Ajax ***/
function listingPagination() {
    jQuery('#listing .pagination a').click(function (e) {
        var page = jQuery(this).html();
        var uri = jQuery(this).attr('href');
        var element = jQuery(this).parents('table');
        jQuery.get(uri, { ajax: true},
            function(data){
                element.html(data);
        });
        element.css('position','relative');
        element.animate({
            opacity: 0,
            left: '-1000'
            }, 800, function() {
            jQuery(this).css('left', 1000);
            jQuery(this).animate({
                opacity: 1,
                left: '0'
                }, 800, function() { jQuery(this).css('position', 'inherit'); listingPagination();});
        });
        e.preventDefault();
    });
}
/*** / Listing Ajax ***/